﻿using AutoMapper;
using JuCheap.Core.Infrastructure;
using JuCheap.Core.IService;
using JuCheap.Core.IService.AppService;
using JuCheap.Core.Model;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JuCheap.Core.WebApi.Controllers
{
    /// <summary>
    /// 角色服务api
    /// </summary>
    [Route("api/[controller]")]
    public class RoleController : BaseApiController
    {
        private readonly ISysRoleService _sysRoleService;
        private readonly IRoleService _roleService;
        private readonly IMapper _mapper;

        /// <summary>
        /// ctor
        /// </summary>
        public RoleController(ISysRoleService sysRoleService, IRoleService roleService, IMapper mapper)
        {
            _sysRoleService = sysRoleService;
            _roleService = roleService;
            _mapper = mapper;
        }

        /// <summary>
        /// 创建角色信息
        /// </summary>
        /// <param name="roleCreateDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<string>> Create([FromBody]RoleCreateRequestDto roleCreateDto)
        {
            var roleId = await _roleService.CreateAsync(roleCreateDto);
            return Ok(roleId);
        }

        /// <summary>
        /// 分页获取角色信息
        /// </summary>
        [HttpPost]
        [Route("search")]
        public async Task<ActionResult<PagedResult<SysRoleDto>>> Search([FromBody]SysRoleFilter filter)
        {
            var records = await _roleService.QueryPagesAsync(filter);
            return Ok(records);
        }

        /// <summary>
        /// 获取角色信息
        /// </summary>
        [HttpGet]
        [Route("{id}")]
        public async Task<ActionResult<SysRoleDto>> Get([FromRoute]string id)
        {
            var roleDto = await _sysRoleService.FindAsync(id);
            return Ok(roleDto);
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleEditDto"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("edit")]
        public async Task<ActionResult<bool>> Edit([FromBody]RoleEditRequestDto roleEditDto)
        {
            await _roleService.UpdateAsync(roleEditDto);
            return Ok(true);
        }

        /// <summary>
        /// 删除角色信息
        /// </summary>
        /// <param name="ids">id集合</param>
        /// <returns></returns>
        [HttpPost]
        [Route("delete")]
        public async Task<ActionResult<bool>> Delete([FromBody]IList<string> ids)
        {
            await _sysRoleService.DeleteAsync(ids);
            return Ok(true);
        }

        /// <summary>
        /// 获取角色菜单Id集合
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{roleId}/menuids")]
        public async Task<ActionResult<IEnumerable<string>>> GetMenuIds([FromRoute]string roleId)
        {
            var menuIds = await _roleService.GetRoleMenuIdsAysnc(roleId);
            return Ok(menuIds);
        }

        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="req">用户角色信息</param>
        /// <returns></returns>
        [HttpPost]
        [Route("set/menu")]
        public async Task<ActionResult<bool>> SetMenus([FromBody]RoleMenuSetRequestDto req)
        {
            await _roleService.SetMenusAsync(req);
            return Ok(true);
        }

        /// <summary>
        /// 获取角色树
        /// </summary>
        [HttpGet]
        [Route("tree")]
        public async Task<ActionResult<IEnumerable<TreeResponseDto>>> GetRoleTree()
        {
            var filter = new SysRoleFilter
            {
                PageIndex = 1,
                PageSize = 10000
            };
            var roles = await _sysRoleService.QueryPagesAsync(filter, x => true);
            var treeList = roles.Datas.Select(x => new TreeResponseDto
            {
                Key = x.Id,
                Title = x.Name,
                Children = null
            });
            return Ok(treeList);
        }
    }
}
